<template>
    <div class="address-new">
        <van-nav-bar left-arrow title="新建收货地址" @click-left="handleRouteBack"/>
        <white-space/>
        <address-form :loading="loading" :address="address" @submit="handleSubmitAdd"/>
    </div>

</template>

<script>
  import { customer, message, security } from "@mall/api-services"
  import AddressForm from "@/views/address/AddressForm"
  import { NavBar, Toast } from "vant"
  import WhiteSpace from "@/components/WhiteSpace"

  export default {
    name: "AddressNew",
    components: {
      AddressForm,
      WhiteSpace,
      [NavBar.name]: NavBar,
    },
    data() {
      return {
        loading: false,
        address: new customer.CustomerAddress(),
      }
    },
    methods: {
      handleRouteBack() {
        this.$router.back()
      },
      handleSubmitAdd(values) {
        const customerId = security.SecurityUserHolder.getUserId()
        this.loading = true
        customer.CustomerAddressServiceV1
          .addAddress(customerId, values)
          .then(() => Toast.success("添加成功"))
          .then(() => this.$router.back())
          .catch(({ response }) => Toast(message.getResponseMessage(response)))
          .finally(() => this.loading = false)
      },
    },
  }
</script>

<style lang="scss" scoped>
    .address-new {

    }
</style>
